<?php

namespace util;

use app\model\UserInfo;
use constant\RedisConstant;

class WorkWxTool
{
    //错误推送地址
    const ErrorPush = '';

    /**
     * ex:
     * 错误通知
     * @param $type
     * @param $msg
     * @return void
     */
    public static function ErrorMsg($type, $msg)
    {
        $msg = $type . "\n" . $msg;
        $content = [
            'content' => $msg,
            'mentioned_list' => ['all']
        ];
        $data['msgtype'] = 'text';
        $data['text'] = $content;
        $options['headers'] = ['Content-Type: application/json'];
        $options['data'] = json_encode($data);
        httpCurl('post', self::ErrorPush, $options);
    }


    /**
     * 群消息发送类
     * @param $url
     * @param $type int 1：文本消息 2：markdown消息 3：image消息 4：文件消息 5：模版卡片(文本) 6:图文消息
     * @param $content
     * @return bool
     */
    public static function noticeToWork($url, $type, $content)
    {
        if (empty($content)) {
            return false;
        }
        if (!$url) {
            return false;
        }
        switch ($type) {
            case 1:
                /**
                 * 文本消息
                 */
                $data['msgtype'] = 'text';
                $data['text'] = $content;
                break;
            case 2:
                /**
                 * markdown 消息
                 */
                $data['msgtype'] = 'markdown';
                $data['markdown'] = $content;
                break;
            case 3:
                /**
                 * image消息
                 */
                $data['msgtype'] = 'image';
                $data['image'] = $content;
                break;
            case 4:
                /**
                 * 文件消息
                 */
                $data['msgtype'] = 'file';
                $data['file'] = $content;
                break;
            case 5:
                /**
                 * 模版卡片
                 * 文本
                 */
                $data['msgtype'] = 'template_card';
                $data['template_card'] = $content;
                break;
            case 6:
                /**
                 * 图文消息
                 */
                $data['msgtype'] = 'template_card';
                $data['template_card'] = $content;
                break;
            default:
                return false;
        }
        $options['headers'] = ['Content-Type: application/json'];
        $options['data'] = json_encode($data);
        $res = httpCurl('post', $url, $options);
        $res = @json_decode($res, true);
        if (isset($res['errcode']) && $res['errcode'] == 0) {
            return true;
        }
        return false;
    }


    /**
     * 群推送
     * @param array $data type,url,content
     * @return bool
     */
    public static function GroupMessagePush(array $data): bool
    {
        switch ($data['type']) {
            case 'text':
                //文本消息
                $data['msgtype'] = 'text';
                $data['text'] = $data['content'];
                break;
            case 2:
                //markdown 消息
                $data['msgtype'] = 'markdown';
                $data['markdown'] = $data['content'];
                break;
            case 3:
                //image消息
                $data['msgtype'] = 'image';
                $data['image'] = $data['content'];
                break;
            case 4:
                //文件消息
                $data['msgtype'] = 'file';
                $data['file'] = $data['content'];
                break;
            case 5:
                //模版卡片 * 文本
                $data['msgtype'] = 'template_card';
                $data['template_card'] = $data['content'];
                break;
            case 6:
                //图文消息
                $data['msgtype'] = 'template_card';
                $data['template_card'] = $data['content'];
                break;
            default:
                return false;
        }
        $options['headers'] = ['Content-Type: application/json'];
        $options['data'] = json_encode($data);
        $res = httpCurl('post', $data['url'], $options);
        $res = json_decode($res, true);
        if ($res['errcode'] == 0) return true;
        return false;
    }

    /**
     * 获取企业微信access_token
     * @return array
     */
    public static function GetWorkAccessToken($WORKID, $SECRET)
    {
        $access_token = RedisTool::get_time(RedisConstant::WorkAccessToken . $SECRET, 'config');
        if ($access_token)
            return [true, $access_token];
        else {
            $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$WORKID&corpsecret=$SECRET";
            $res = httpGet($url);
            if (is_array($res)) {
                if ($res['errcode'] == 0) {
                    $access_token = $res['access_token'];
                    $expires_in = $res['expires_in'];//凭证有效时间，单位秒，正常为7200
                    RedisTool::set_time(RedisConstant::WorkAccessToken . $SECRET, $access_token, $expires_in - 100, 'config');
                    return [true, $access_token];
                }
            }
            return [false, '获取企业微信接口调用凭证失败!'];
        }
    }
}